/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.cv4j.app.slice;

import com.cv4j.app.ResourceTable;
import com.cv4j.app.app.BaseAbilitySlice;
import com.cv4j.core.datamodel.CV4JImage;
import com.cv4j.core.datamodel.ImageProcessor;
import com.cv4j.core.datamodel.Rect;
import com.cv4j.core.pixels.Operator;

import ohos.aafwk.content.Intent;
import ohos.agp.components.Image;

/**
 * PixelOperatorFragment
 *
 * @since 2021-05-14
 */
public class SubImageAbilitySlice extends BaseAbilitySlice {
    @Override
    protected int getUIContentLayoutId() {
        return ResourceTable.Layout_ability_sub_image;
    }

    @Override
    protected void initComponents(Intent intent) {
        initView();
    }

    private void initView() {
        Image resultimg = (Image) findComponentById(ResourceTable.Id_result_image1);
        Image image = (Image) findComponentById(ResourceTable.Id_sub_image);
        image.setPixelMap(ResourceTable.Media_pixel_test_3);
        CV4JImage cv4jImage = new CV4JImage(image.getPixelMap());
        final ImageProcessor imageProcessor = cv4jImage.getProcessor();
        Rect rect = new Rect();
        final int max = 300;
        final int min = 200;
        final int height = 450;
        rect.x1 = max;
        rect.y1 = min;
        rect.width = max;
        rect.height = height;
        ImageProcessor resultImageProcessor = null;
        resultImageProcessor = Operator.subImage(imageProcessor, rect);
        if (resultImageProcessor != null) {
            CV4JImage resultcv4jImage = new CV4JImage(resultImageProcessor.getWidth(),
                resultImageProcessor.getHeight(), resultImageProcessor.getPixels());
            resultimg.setPixelMap(resultcv4jImage.getProcessor().getImage().toBitmap());
        }
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
